package com.sunny.ynhz.utils;

import android.app.Activity;
import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;

import com.sunny.ynhz.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by huhuan123 on 2018/5/21.
 */

public class SwitchFragmentUtils {
    Context context;
    public Fragment nextFragment;
    FragmentManager supportFragmentManager;

    public SwitchFragmentUtils(Context context, FragmentManager supportFragmentManager) {
        this.context = context;
        this.supportFragmentManager = supportFragmentManager;
    }

    /**
     * @param nowFragment 当前的fragment
     */
    public void swithFragment(Fragment nowFragment) {
        FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
        if (nextFragment == null) {
            fragmentTransaction.add(R.id.fragments, nowFragment).commit();
        } else {
            if (nowFragment.isAdded()) {
                fragmentTransaction.hide(nextFragment).show(nowFragment).commit();
            } else {
                fragmentTransaction.hide(nextFragment).add(R.id.fragments, nowFragment).commit();
            }
        }
        nextFragment = nowFragment;
    }


}
